/**
 * DataSource API Service
 *
 * 数据源管理API服务 (FR-001 ~ FR-008)
 * - 数据源CRUD操作
 * - 连接测试
 * - 启用/禁用
 * - 软删除
 */

import { get, post, put, del } from './client'
import type {
  ApiResponse,
  PageResponse,
  DataSource,
  DataSourceCreateRequest,
  DataSourceUpdateRequest,
  DataSourceTestRequest,
  DataSourceTestResponse,
  DataSourceListParams,
} from '../models/types'

/**
 * 获取数据源列表 (分页) - FR-001
 */
export const listDataSources = async (
  params: DataSourceListParams
): Promise<ApiResponse<PageResponse<DataSource>>> => {
  return get<PageResponse<DataSource>>('/datasources', { params })
}

/**
 * 获取数据源详情 - FR-001
 */
export const getDataSource = async (id: number): Promise<ApiResponse<DataSource>> => {
  return get<DataSource>(`/datasources/${id}`)
}

/**
 * 创建数据源 - FR-002
 */
export const createDataSource = async (
  data: DataSourceCreateRequest
): Promise<ApiResponse<DataSource>> => {
  return post<DataSource>('/datasources', data)
}

/**
 * 更新数据源 - FR-003
 */
export const updateDataSource = async (
  id: number,
  data: DataSourceUpdateRequest
): Promise<ApiResponse<DataSource>> => {
  return put<DataSource>(`/datasources/${id}`, data)
}

/**
 * 删除数据源 (软删除) - FR-004
 */
export const deleteDataSource = async (id: number): Promise<ApiResponse<void>> => {
  return del<void>(`/datasources/${id}`)
}

/**
 * 测试数据源连接 - FR-005
 */
export const testDataSourceConnection = async (
  data: DataSourceTestRequest
): Promise<ApiResponse<DataSourceTestResponse>> => {
  return post<DataSourceTestResponse>('/datasources/test-connection', data)
}

/**
 * 启用数据源 - FR-006
 */
export const enableDataSource = async (id: number): Promise<ApiResponse<DataSource>> => {
  return put<DataSource>(`/datasources/${id}/enable`, {})
}

/**
 * 禁用数据源 - FR-007
 */
export const disableDataSource = async (id: number): Promise<ApiResponse<DataSource>> => {
  return put<DataSource>(`/datasources/${id}/disable`, {})
}

/**
 * 批量操作 - 启用多个数据源 - FR-008
 */
export const batchEnableDataSources = async (ids: number[]): Promise<ApiResponse<void>> => {
  return post<void>('/datasources/batch/enable', { ids })
}

/**
 * 批量操作 - 禁用多个数据源 - FR-008
 */
export const batchDisableDataSources = async (ids: number[]): Promise<ApiResponse<void>> => {
  return post<void>('/datasources/batch/disable', { ids })
}

/**
 * 批量操作 - 删除多个数据源 - FR-008
 */
export const batchDeleteDataSources = async (ids: number[]): Promise<ApiResponse<void>> => {
  return del<void>('/datasources/batch', { data: { ids } })
}
